class IndexController < ApplicationController
  def index
    if request.get? && !params[:inviteid].nil?
    	invite = Invitation.find(:first,:conditions=>"hashkey='#{params[:inviteid]}' ")   
    	unless invite.nil? || invite.friend_id>0 
    		params[:email]=invite.email
    		params[:inviteid]=invite.id
    	else
    		params[:inviteid]=''
    	end
    end
  	@hotfiles=Hotfile.find(:all,:order => "clicked DESC",:conditions=>"status=1")
    @manager=NetManager.new(session[:user_id]) unless session[:user_id].nil?
    @topest_users=ViewUserCount.find(:all,:order => "usercount DESC",:limit =>5, :conditions=>["is_public=1  AND usercount>0 AND LENGTH(interests)>12"])
    @newest_users=ViewUserCount.find(:all,:order => "id DESC",:limit =>5, :conditions=>["is_public=1  AND usercount>0 AND LENGTH(interests)>12"])
  end
  def login   
    session[:user_id] = nil  
    if request.post? 
      user = User.find_by_email_and_password(params[:email], params[:pwd])   
      if user.nil?
        @email=params[:email]
        flash[:notice] = 'password wrong or user not exists!'   
        redirect_to( :action=>'index')   
      else
	    #save his google adsense id
	    unless user.invited_by==0
	    	he = User.find(invite.invited_by)
	    	session[:googleadid]=he.adsense_id unless he.nil?
	    end
        session[:user_id] = user.id.to_s
        session[:user_nick]=user.login+rand(100).to_s
        redirect_to :controller=>'search', :action=>'index',:server=>session[:user_id]
      end
    end
  end
  def logout
  	session[:user_id]=nil
  	session[:user_nick]=nil
  	session[:googleadid]=nil
  	redirect_to( :action=>'index')
  end
  def joinf
  	begin
	  	ffile=Hotfile.find(params[:id].to_i)
  	rescue
  		ffile=nil
  	end
  	redirect_to( :action=>'index') if ffile.nil?
  	ffile.clicked=ffile.clicked+1
  	ffile.save
  	hf=HotfileClick.new
  	hf.create_at=Time.now
  	hf.is_used=0
  	hf.swarm_id=ffile.user_id
  	hf.name=ffile.name
  	hf.file_id=ffile.id
  	hf.filesizelong=ffile.filesizelong
  	hf.hashkey=ffile.filehash
  	hf.description=ffile.description
  	
  	if !(session[:user_id].nil?)
	  	hf.user_id=session[:user_id].to_i
	  	hf.save
	  	redirect_to( :action=>'index')
  	else
    	unless (cookies[:guest].nil?||cookies[:guest].blank?)
	    	user = User.find_by_email_and_password(cookies[:guest].to_s, cookies[:guest].to_s.split('@')[0])
	    	unless user.nil? || user.is_guest==1
	    		#login in
		        session[:user_id] = user.id.to_s
		        session[:user_nick]=user.login+rand(100).to_s
		        redirect_to :controller=>'search', :action=>'index',:server=>session[:user_id]
		        return
	    	end
    	end
    	params[:des]=''
    	is_guest=1
    	while true
	    	params[:email]='kerjo'+rand(99999999).to_s+'@email.com'
	    	user = User.find_by_email(params[:email])
	    	break if user.nil?
    	end
    	cookies[:guest]=params[:email]
    	params[:pwd]=params[:email].to_s.split('@')[0]
    	
	    require 'digest/md5'
	    @user=User.new()
	    @user.is_guest=is_guest
	    @user.login=params[:email].to_s.split('@')[0]
	    @user.email=params[:email]
	    @user.interests=params[:des]
	    @user.password=params[:pwd]
	    @user.hashkey=(Digest::MD5).hexdigest(params[:email]+session.session_id.to_s).to_s
	    @user.save
	    manager=NetManager.new(@user.id)
	    
	    #join three default network
    	users=ViewUserCount.find(:all,:conditions=>'is_public=1',:order => "usercount DESC",:limit =>3)
    	unless users.nil?
    		users.each{|u| 
		  		manager.add_friend(u.id)
    		}
    	end
	    session[:user_id] = @user.id.to_s
	    session[:user_nick]=@user.login+rand(100).to_s
	    
	  	hf.user_id=session[:user_id].to_i
	  	hf.save
        	    	
  	
	    redirect_to :controller=>'search', :action=>'index',:server=>session[:user_id]
  	end
    return
  end
  def join
  	#for url join
  	join_by_url=false
   	is_guest=0
  	if request.post? && !params[:id].nil? && !params[:id].blank?
	  	join_by_url=true
    	params[:des]=''
    	is_guest=1
    	while true
	    	params[:email]='kerjo'+rand(99999999).to_s+'@email.com'
	    	user = User.find_by_email(params[:email])
	    	break if user.nil?
    	end
    	cookies[:guest]=params[:email]
    	params[:pwd]=params[:email].to_s.split('@')[0]  		
  	else
	  	#for email join
	    user = User.find_by_email(params[:email].strip!)   
	    unless user.nil?
	      flash[:notice] = 'user exists!'   
	      redirect_to( :action=>'index') 
	      return  
	    end
	end
    
    #for guest
    #is_guest=0
    if(params[:email].to_s=="Guests leave blank")
    	unless (cookies[:guest].nil?||cookies[:guest].blank?)
	    	user = User.find_by_email_and_password(cookies[:guest].to_s, cookies[:guest].to_s.split('@')[0])
	    	unless user.nil? || user.is_guest==1
	    		#login in
		        session[:user_id] = user.id.to_s
		        session[:user_nick]=user.login+rand(100).to_s
		        redirect_to :controller=>'search', :action=>'index',:server=>session[:user_id]
		        return
	    	end
    	end
    	params[:des]=''
    	is_guest=1
    	while true
	    	params[:email]='kerjo'+rand(99999999).to_s+'@email.com'
	    	user = User.find_by_email(params[:email])
	    	break if user.nil?
    	end
    	cookies[:guest]=params[:email]
    	params[:pwd]=params[:email].to_s.split('@')[0]
    end
    require 'digest/md5'
    @user=User.new()
    @user.is_guest=is_guest
    @user.login=params[:email].to_s.split('@')[0]
    @user.email=params[:email]
    @user.interests=params[:des]
    @user.password=params[:pwd]
    @user.hashkey=(Digest::MD5).hexdigest(params[:email]+session.session_id.to_s).to_s
    @user.save
    manager=NetManager.new(@user.id)
    
    #for join_by_url
    add_friend=false
    unless(params[:inviteuserhash].nil? || params[:inviteuserhash].blank?)
	    he = User.find_by_hashkey(params[:inviteuserhash])
	    unless he.nil?
	   		@user.invited_by=he.id
   			@user.save
		    manager.add_friend_with_notify(he.id,request)
   			session[:googleadid]=he.adsense_id
		    #he add me as a friend//no message
	  		friend=Friend.new
	  		friend.user_id=he.id
	  		friend.friend_id=@user.id
	  		friend.save
	  		add_friend=true
   		end 
	end
	    
    if join_by_url && !add_friend
	    he = User.find(params[:id])
	    unless he.nil?
	   		@user.invited_by=he.id
   			@user.save
   			manager.add_friend(he.id)
   			session[:googleadid]=he.adsense_id
   			add_friend=true
   		end 
    end

    unless(params[:inviteid].nil? || params[:inviteid].blank?)
    	invite = Invitation.find(params[:inviteid])  
    	unless invite.nil?
    		@user.invited_by=invite.user_id
    		@user.save

    		#save invite
    		invite.friend_id=@user.id.to_s
    		invite.save

    		#save his google adsense id
			he = User.find(invite.user_id)
    		session[:googleadid]=he.adsense_id unless he.nil?
    		
    		#add him as a friend
		    manager.add_friend_with_notify(invite.user_id,request)
		    #he add me as a friend//no message
	  		friend=Friend.new
	  		friend.user_id=invite.friend_id
	  		friend.friend_id=invite.user_id
	  		friend.save
	  		add_friend=true
    	end
    end
    
    #add threee default friends
    unless add_friend
	    #join three default network
    	users=ViewUserCount.find(:all,:conditions=>'is_public=1',:order => "usercount DESC",:limit =>3)
    	unless users.nil?
    		users.each{|u| 
		  		manager.add_friend(u.id)
    		}
    	end
    end
    session[:user_id] = @user.id.to_s
    session[:user_nick]=@user.login+rand(100).to_s
    redirect_to :controller=>'search', :action=>'index',:server=>session[:user_id]
  end
end